home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0028_More Prime Numbers.pas < prev    next >
Pascal/Delphi Source File  |  1993-08-27  |  1KB  |  50 lines

  1. {
  2. JONATHAN WRIGHT
  3.  
  4. Here is source For finding primes.  I just pulled this off of an OLD backup
  5. disk, so I don't Really know how optimized it is, but it works:
  6. }
  7.  
  8. Const
  9.   FirstPrime = 2;
  10.   MaxPrimes  = 16000; (* Limit 64k For one Array, little more work For more *)
  11.  
  12. Var
  13.   Primes      : Array [1..MaxPrimes] of LongInt;
  14.  
  15.   PrimesFound : LongInt;
  16.   TestNumber  : LongInt;
  17.   Count       : LongInt;
  18.  
  19.   IsPrime     : Boolean;
  20.  
  21. begin
  22.   PrimesFound := 1;
  23.   TestNumber  := FirstPrime + 1;
  24.  
  25.   For Count := 1 to MaxPrimes DO
  26.     Primes[Count] := 0;
  27.  
  28.   Primes[1] := FirstPrime;
  29.  
  30.   Repeat
  31.     Count   := 1;
  32.     IsPrime := True;
  33.  
  34.     Repeat
  35.       if Odd (TestNumber) then
  36.         if TestNumber MOD Primes[Count] = 0 then
  37.           IsPrime := False;
  38.           INC (Count);
  39.     Until (IsPrime = False) or (Count > PrimesFound);
  40.  
  41.     if IsPrime = True then
  42.     begin
  43.       INC (PrimesFound);
  44.       Primes[PrimesFound] := TestNumber;
  45.       Write (TestNumber, ', ');
  46.     end;
  47.     INC (TestNumber);
  48.   Until PrimesFound = MaxPrimes;
  49. end.
  50.